$(function(){
    const $number = $("input:eq(0)");
    const $company = $("input:eq(1)");
    const $username = $("input:eq(2)");
    const $userphone = $("input:eq(3)");
    const $labelPhone = $("label[class=layui-form-label]:eq(3)");
    const $labelNumber = $("label[class=layui-form-label]:eq(0)");

    const $labelSend = $("#send-message-label");
    const $spanSend = $("#send-message-span");
    const $inputSend = $("#send-message-checkbox");
    const $divSend = $("#send-message-label div");

    $labelSend.hide();

    let showCheckBox = function() {
        if (test_ipt(testPhone,$userphone,$labelPhone)) {
            $spanSend.text(' 录入成功发送短信给 ' + $userphone.val());
            $inputSend.prop('checked', true);
            $divSend.addClass('layui-form-checked');
            $labelSend.show();
        } else {
            $inputSend.prop('checked', false);
            $divSend.removeClass('layui-form-checked');
            $labelSend.hide();
        }
    }

    $number.bind('input propertychange', function () {
        test_ipt(testNumber,$number,$labelNumber);
    })
    $userphone.bind('input propertychange', showCheckBox)
    $labelSend.click(function () {
        if ($divSend.hasClass('layui-form-checked')) {
            $inputSend.prop('checked', false);
            $divSend.removeClass('layui-form-checked');
        } else {
            $inputSend.prop('checked', true);
            $divSend.addClass('layui-form-checked');
        }
    });

    $("form").submit(function(){
        $number.val($number.val().trim());
        if (!test_ipt(testNumber,$number,$labelNumber)){
            layer.msg('快递单号格式有误');
            return false;
        }
        if (!test_ipt(testPhone,$userphone,$labelPhone)){
            layer.msg('请输入正确的手机号码');
            return false;
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/express/add.do", {
            number:$number.val(),
            company:$company.val(),
            username:$username.val(),
            userphone:$userphone.val(),
            sendMessage:$inputSend.prop('checked')
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status === 0) {
                $number.val("")
                $company.val("顺丰速运")
                $username.val("")
                $userphone.val("")
                $labelSend.hide();
            }
        });
        return false;
    });
    $("button[type=reset]").click(function () {$labelSend.hide();});
});